//
//  HomeViewController.m
//  TriumphInTheSkiesAPP
//
//  Created by 要帅气的生活 on 15/11/3.
//  Copyright © 2015年 UNOHACHA. All rights reserved.
//

#import "HomeViewController.h"
#import "Head.h"
#import "ScrollAnPageControl.h"
#import "UIImageView+WebCache.h"
#import "UIButton+WebCache.h"
#import "HomeTableViewCell.h"
#import "GundongToutiao.h"
#import "TouTiaoViewController.h"
#import "LunBoTuViewController.h"
#import "TwoViewController.h"
#import "LoginViewController.h"
#import "ZhongJianDiZhiViewController.h"

@interface HomeViewController ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>

{
    CGRect frame_first;
    
    UIImageView *fullImageView;
    
    BOOL flag;

}

@property (nonatomic,retain) UITableView *table;
@property (nonatomic,retain) ScrollAnPageControl *scroll;
@property (nonatomic,retain) GundongToutiao *toutiao;
//用来播放头条
@property (nonatomic,retain) UIScrollView *scroll2;
@property (nonatomic,retain) UIPageControl *page;
@property (nonatomic,retain) NSArray *arr;
@property (nonatomic,retain) NSArray *arrToutiao;
@property (nonatomic,strong) UIView *headView;
@property (nonatomic,strong) UIButton *buttonLeft;
@property (nonatomic,strong) UIButton *buttonRight;
@property (nonatomic,strong) BaseView *zhongView;
@property (nonatomic,strong) NSTimer *timerLunBuTu;
@end

@implementation HomeViewController
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
 
    flag = YES;
//    [General initialization:self isPoint:YES];

    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:101.0f/255.0f green:192.0f/255.0f blue:240.0f/255.0f alpha:1];
    self.navigationController.navigationBar.barStyle = 1;
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    //隐藏导航栏
    self.navigationController.navigationBarHidden = YES;
    
    //放轮播图片的数组
    self.arr = @[@"lunbotu1.jpg",@"lunbotu2.jpg",@"lunbotu3.jpg",@"lunbotu4.jpg",@"lunbotu5.jpg"];
    //放滚动头条图片的数组
    self.arrToutiao = @[@"gundong1.png",@"gundong2.png",@"gundong3.png",@"gundong4.png"];
    
    UIView *head = [[UIView alloc] initWithFrame:CGRectMake(0, -20, WindowWidth, WindowHeight/3+40)];
    //[self.view addSubview:head];
    
    //滚动头条
    self.toutiao = [[GundongToutiao alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height/3, self.view.frame.size.width, 40)];
    self.toutiao.scroll.delegate = self;
    [self.toutiao.page addTarget:self action:@selector(toutiaoPage) forControlEvents:UIControlEventValueChanged];
    [head addSubview:self.toutiao];
    self.toutiao.scroll.tag = 21;
    [self.toutiao addTarget:self selector:@selector(gundong)];
    
    BaseView *view4 = [[BaseView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width/4*3, self.toutiao.frame.size.height)];
    [self.toutiao.scroll addSubview:view4];
    view4.imgaeview.image = [UIImage imageNamed:self.arrToutiao[3]];
    
    for (int i = 0; i < self.arrToutiao.count; i++) {
        BaseView *view5 = [[BaseView alloc] initWithFrame:CGRectMake(0, 40*(i+1), self.view.frame.size.width/4*3, 40)];
        [self.toutiao.scroll addSubview:view5];
        view5.imgaeview.image = [UIImage imageNamed:self.arrToutiao[i]];
    }
    BaseView *view6 = [[BaseView alloc] initWithFrame:CGRectMake(0, self.toutiao.frame.size.height*5, self.view.frame.size.width/4*3, self.toutiao.frame.size.height)];
    [self.toutiao.scroll addSubview:view6];
    view6.imgaeview.image = [UIImage imageNamed:self.arrToutiao[0]];
    
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timer2) userInfo:nil repeats:YES];
    
    
    //轮播图
    self.scroll = [[ScrollAnPageControl alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, 667/3*Height)];
    self.scroll.scroll.delegate = self;
    //self.scroll.scroll.contentOffset = CGPointMake(375*Width, 0);
    [self.scroll.page addTarget:self action:@selector(page:) forControlEvents:UIControlEventValueChanged];
    [head addSubview:self.scroll];
    self.scroll.scroll.tag = 20;
    //设置多少时间跳下一张
    self.timerLunBuTu = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(timer1) userInfo:nil repeats:YES];
    
    
    BaseView *view1 = [[BaseView alloc] initWithFrame:self.scroll.frame];
    [self.scroll.scroll addSubview:view1];
    view1.imgaeview.image = [UIImage imageNamed:self.arr[4]];
    [view1 addTarget:self selector:@selector(buttonLunbotu:)];
    view1.tag = 50;
    
    for (int i = 0; i < self.arr.count; i++) {
        BaseView *view2 = [[BaseView alloc] initWithFrame:CGRectMake(375*(i+1)*Width, 0, self.view.frame.size.width, self.view.frame.size.height/3)];
        [self.scroll.scroll addSubview:view2];
        view2.imgaeview.image = [UIImage imageNamed:self.arr[i]];
        [view2 addTarget:self selector:@selector(buttonLunbotu:)];
        view2.tag = 51+i;
    }
    BaseView *view3 = [[BaseView alloc] initWithFrame:CGRectMake(375*6*Width, 0, self.view.frame.size.width, self.view.frame.size.height/3)];
    [self.scroll.scroll addSubview:view3];
    view3.imgaeview.image = [UIImage imageNamed:self.arr[0]];
    [view3 addTarget:self selector:@selector(buttonLunbotu:)];
    view3.tag = 56;
    
    //轮播图上面的按钮点击事件
    [self.scroll.buttonLeft addTarget:self action:@selector(dianji:) forControlEvents:UIControlEventTouchUpInside];
    self.scroll.buttonLeft.tag = 30;
    
    [self.scroll.buttonRight addTarget:self action:@selector(dianji:) forControlEvents:UIControlEventTouchUpInside];
    self.scroll.buttonRight.tag = 31;
    
    [self.scroll.zhongView addTarget:self selector:@selector(didian)];
    
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, -20, WindowWidth, WindowHeight+40) style:UITableViewStyleGrouped];
    [self.view addSubview:self.table];
    self.table.tableHeaderView = head;
    self.table.delegate = self;
    self.table.dataSource = self;
    self.table.rowHeight = 100;
    /* 无分割线 */
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.table registerClass:[HomeTableViewCell class] forCellReuseIdentifier:@"LOL"];
    
    [self HeadAddView];
    
    [self.table addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:@"aa"];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    CGPoint point = [[change objectForKey:@"new"] CGPointValue];
//    NSLog(@"%@",change);
//    NSLog(@"%f",point.y);
    if (point.y >120) {
        self.headView.alpha = (point.y - 120)/100;
    }
    if (point.y <120) {
        self.headView.alpha = 0;
    }
    
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    //table的滑动方法
    
}
-(void)timer1
{
    if (self.scroll.page.currentPage == 4) {
        self.scroll.page.currentPage = 0;
        [self.scroll.scroll setContentOffset:CGPointMake(self.view.frame.size.width*6,0) animated:YES];
        
        
        if (self.scroll.scroll.contentOffset.x == self.view.frame.size.width*5)
        {
            self.scroll.scroll.contentOffset = CGPointMake(0, 0);
        }
    }else{
        [self.scroll.scroll setContentOffset:CGPointMake((self.scroll.page.currentPage+2) * self.view.frame.size.width ,0) animated:YES];
        self.scroll.page.currentPage += 1;
    }
}
-(void)page:(UIPageControl *)page
{
    [self.scroll.scroll setContentOffset:CGPointMake((self.scroll.page.currentPage+1) * self.view.frame.size.width ,0) animated:YES];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.tag == 20) {
        if (self.scroll.scroll.contentOffset.x == 0) {
            self.scroll.scroll.contentOffset = CGPointMake(375*5*Width, 0);
        }
        if (self.scroll.scroll.contentOffset.x == 375 *6*Width) {
            self.scroll.scroll.contentOffset = CGPointMake(375*Width, 0);
        }
        self.scroll.page.currentPage = scrollView.contentOffset.x / self.view.frame.size.width -1;
    }
    if (scrollView.tag == 21) {
        if (self.toutiao.scroll.contentOffset.y == 0) {
            self.toutiao.scroll.contentOffset = CGPointMake(0, 40*4);
        }
        if (self.toutiao.scroll.contentOffset.y == 40*5) {
            self.toutiao.scroll.contentOffset = CGPointMake(0, 40);
        }
        self.toutiao.page.currentPage = scrollView.contentOffset.y / 40 -1;
    }
}


-(void)timer2
{
    if (self.toutiao.page.currentPage == 3) {
        self.toutiao.page.currentPage = 0;
        [self.toutiao.scroll setContentOffset:CGPointMake(0,40*5) animated:YES];
        if (self.toutiao.scroll.contentOffset.y == 40*4)
        {
            self.toutiao.scroll.contentOffset = CGPointMake(0, 0);
        }
    }else{
        [self.toutiao.scroll setContentOffset:CGPointMake(0 ,(self.toutiao.page.currentPage+2) * 40) animated:YES];
        self.toutiao.page.currentPage += 1;
    }
}
-(void)toutiaoPage
{
    [self.toutiao.scroll setContentOffset:CGPointMake(0 ,(self.toutiao.page.currentPage+1) * 40) animated:YES];
}



//轮播图点击事件
-(void)buttonLunbotu:(BaseView *)view{
    LunBoTuViewController *lunbotu = [[LunBoTuViewController alloc] init];
    if (view.tag == 51 || view.tag == 56) {
        NSLog(@"1");
        lunbotu.str = self.arr[0];
    }
    if (view.tag == 52) {
        NSLog(@"2");
        lunbotu.str = self.arr[1];
    }
    if (view.tag == 53) {
        NSLog(@"3");
        lunbotu.str = self.arr[2];
    }
    if (view.tag == 54) {
        NSLog(@"4");
        lunbotu.str = self.arr[3];
    }
    if (view.tag == 55 || view.tag == 50) {
        NSLog(@"5");
        lunbotu.str = self.arr[4];
    }
    [self.navigationController pushViewController:lunbotu animated:YES];
    lunbotu.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = YES;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LOL"];
    cell.pic.image = [UIImage imageNamed:[NSString stringWithFormat:@"shouye%ld.jpg",indexPath.row+1]];
//    cell.pic.tag=9999;
//    cell.pic.userInteractionEnabled=YES;
//    [cell.pic addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(actionTap:)]];
//    UIImageView *image=(UIImageView *)[cell.contentView viewWithTag:9999];
//    image.userInteractionEnabled=YES;
//    
//    image.image=[UIImage imageNamed:[NSString stringWithFormat:@"shouye%ld.jpg",indexPath.row+1]];
    
    
    cell.title.text = @"杭州市拱墅区，东方明珠小区开盘了！";
    cell.shuoming.text = @"时间：2015-12-20";
    //cell
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"666");
    TwoViewController *two = [[TwoViewController alloc] init];
    [self.navigationController pushViewController:two animated:YES];
    two.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = NO;
    
}
-(void)actionTap:(UITapGestureRecognizer *)sender{
    
    if (flag) {
        
    
    
    CGPoint location = [sender locationInView:self.table];
    NSIndexPath *indexPath  = [self.table indexPathForRowAtPoint:location];
    
    UITableViewCell *cell = (UITableViewCell *)[self.table  cellForRowAtIndexPath:indexPath];
    
    
    //     NSLog(@"%f",self.tableView.contentOffset.y);
    //
    //
    //    NSLog(@"%f----%f-----%f-----%f",cell.frame.origin.x,cell.frame.origin.y-self.tableView.contentOffset.y,cell.frame.size.height,cell.frame.size.width);
    //
    //
    
    
    UIImageView *imageView=(UIImageView *)[cell.contentView viewWithTag:9999];
    
    
    frame_first=CGRectMake(cell.frame.origin.x+imageView.frame.origin.x, cell.frame.origin.y+imageView.frame.origin.y-self.table.contentOffset.y, imageView.frame.size.width, imageView.frame.size.height);
    
    
    
    
    
    
    
    fullImageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, [UIScreen mainScreen].bounds.size.height)];
    fullImageView.backgroundColor=[UIColor blackColor];
    fullImageView.userInteractionEnabled=YES;
    [fullImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionTap2:)]];
    fullImageView.contentMode=UIViewContentModeScaleAspectFit;
    
    
    
    if (![fullImageView superview]) {
        
        fullImageView.image=imageView.image;
        
        [self.view.window addSubview:fullImageView];
        
        
        
        fullImageView.frame=frame_first;
        [UIView animateWithDuration:0.5 animations:^{
            
            fullImageView.frame=CGRectMake(0, 0, 320, [UIScreen mainScreen].bounds.size.height);
            
            
        } completion:^(BOOL finished) {
            
            [UIApplication sharedApplication].statusBarHidden=YES;
            
        }];
        
        
        
    }
    
    
    
    flag = NO;
    
    }
    
}




-(void)actionTap2:(UITapGestureRecognizer *)sender{
    
    
    if (!flag) {
        
    
    
    [UIView animateWithDuration:0.5 animations:^{
        
        fullImageView.frame=frame_first;
        
        
    } completion:^(BOOL finished) {
        
        [fullImageView removeFromSuperview];
        
        
    }];
    
    
    [UIApplication sharedApplication].statusBarHidden=NO;
    
        flag = YES;
    }
}

//滚动条点击事件
-(void)gundong
{
    NSLog(@"头条");
    TouTiaoViewController *toutiao = [[TouTiaoViewController alloc] init];
    [self.navigationController pushViewController:toutiao animated:YES];
    toutiao.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = NO;
}
//轮播图上面的按钮点击事件
-(void)dianji:(UIButton *)button
{
    MFJNight *njght = [MFJNight defaultNight];
    if (button.tag == 30) {
        NSLog(@"左");
        if (njght.nightKey) {
            NSLog(@"登陆状态");
        }else{
            LoginViewController *login = [[LoginViewController alloc] init];
            [self.navigationController pushViewController:login animated:YES];
            login.view.backgroundColor = [UIColor whiteColor];
        }
    }
    if (button.tag == 31) {
        NSLog(@"右");
        if (njght.nightKey) {
            NSLog(@"登陆状态");
        }else{
            LoginViewController *login = [[LoginViewController alloc] init];
            [self.navigationController pushViewController:login animated:YES];
            login.view.backgroundColor = [UIColor whiteColor];
        }
    }
}
//中间选择地图键
-(void)didian
{
    void (^suml)(NSString *) = ^(NSString *b){
        
        self.label.text = b;
        self.scroll.label.text = b;
    };
    ZhongJianDiZhiViewController *dizhi = [[ZhongJianDiZhiViewController alloc] init];
    dizhi.suml = suml;
    [self.navigationController pushViewController:dizhi animated:YES];
    dizhi.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = NO;
    
}

//最头上的3个按钮
-(void)HeadAddView{
    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, WindowWidth, 84)];
    [self.view addSubview:self.headView];
    self.headView.backgroundColor = [UIColor blueColor];
    //[self.headView setUserInteractionEnabled:NO];
    self.headView.alpha = 0;
    self.buttonLeft = [UIButton buttonWithType:UIButtonTypeSystem];
    self.buttonLeft.frame = CGRectMake(10, 45, 30, 30);
    //self.buttonLeft.backgroundColor = [UIColor blueColor];
    [self.buttonLeft setBackgroundImage:[UIImage imageNamed:@"youjian.png"] forState:UIControlStateNormal];
    [self.headView addSubview:self.buttonLeft];
    
    self.zhongView = [[BaseView alloc] initWithFrame:CGRectMake(130, 45, 95, 30)];
    [self.headView addSubview:self.zhongView];
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 65, 30)];
    [self.zhongView addSubview:self.label];
    self.label.text = @"城市";
    self.label.textAlignment = 2;
    self.label.textColor = [UIColor whiteColor];
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(65, 0, 30, 30)];
    [self.zhongView addSubview:img];
    img.image = [UIImage imageNamed:@"hudong.png"];
    
    self.buttonRight = [UIButton buttonWithType:UIButtonTypeSystem];
    self.buttonRight.frame = CGRectMake(305, 45, 60, 30);
    [self.buttonRight setTitle:@"添加客户" forState:UIControlStateNormal];
    self.buttonRight.tintColor = [UIColor whiteColor];
    [self.headView addSubview:self.buttonRight];
    
    [self.buttonLeft addTarget:self action:@selector(dianji:) forControlEvents:UIControlEventTouchUpInside];
    self.buttonLeft.tag = 30;
    
    [self.buttonRight addTarget:self action:@selector(dianji:) forControlEvents:UIControlEventTouchUpInside];
    self.buttonRight.tag = 31;
    
    [self.zhongView addTarget:self selector:@selector(didian)];
}
@end
